

#include "window.h"



/*---------------------------------------------------------------------------*/



/***
 * window class constructor
 ***/
window::window()
{
   return;
}



/*---------------------------------------------------------------------------*/


/***
 * window class destructor
 ***/
window::~window()
{
   return;
}





/*---------------------------------------------------------------------------*/





/***
 *creates a new button, returns the index of the button
 *in the specified window.
 *last parameter is a pointer to a function
 *this function will handle a click event.
 *the x and y position values will map to
 *the size of the window, if one is greater
 *than the size of the window, it will be placed
 *at the maximum for the window.
 ***/
int window::add_button(char* name, int b_x, int b_y, int b_w, 
                     int b_h, char* image_up, char* image_down, 
                     void (*func_p)())
{  
    button but;
    
    //preset the button image id
    but.up_id = -1;
    but.dn_id = -1;
    
    //get the images for the button
    but.up_id = get_image_id(image_up);
    but.dn_id = get_image_id(image_down);
    
    //if we couldn't find the image in a skin then search the local dir
    if (but.up_id == -1)
       ;
                              
    if (but.dn_id == -1)
       ;
    
    
    but.cur_id = but.up_id;
    
    //make sure we are within the window's limits
    if (b_x + b_w > w)
       b_x = w - b_w;
    if (b_y + b_h > h)
       b_y = h - b_h;
       
    but.x = x + b_x;
    but.y = y + b_y;
    but.h = b_h;
    but.w = b_w;
    but.func_p = func_p;
    strcpy(but.name, name);
    
    //add the button to the window
    buttons.push_back(but);           
                
    return 100*win_id +(int)buttons.size() - 1;
}
  
  
  


/*---------------------------------------------------------------------------*/  
  
  
  
                            
                                       
/***
 *create a textarea and add it to the window.
 *returns the index of the textarea. 
 ***/
int window::add_textarea(char* text, int t_x, int t_y)
{
    textarea tb;
       
    tb.x = x + t_x;
    tb.y = y + t_y;

    tb.text = text;
    texts.push_back(tb);
    
    return (int)texts.size() - 1;
}





/*---------------------------------------------------------------------------*/  
  
  
  
                            
                                       
/***
 * create a textbox and add it to the window.
 * returns the index of the textbox. 
 ***/
int window::add_textbox(int t_w, int t_h, int t_x, int t_y, int t_rgb[3])
{
    textbox tb;
       
    tb.x = x + t_x;
    tb.y = y + t_y;
	
	tb.w = t_w;
	tb.h = t_h;

	tb.rgb[0] = t_rgb[0];
	tb.rgb[1] = t_rgb[1];
	tb.rgb[2] = t_rgb[2];

    textbs.push_back(tb);
    
    return (int)textbs.size() - 1;
}




/*---------------------------------------------------------------------------*/






int window::add_listbox(int t_w, int t_h, int t_x, int t_y, int t_rgb[3])
{
	listbox lb;
       
    lb.x = x + t_x;
    lb.y = y + t_y;
	
	lb.w = t_w;
	lb.h = t_h;

	lb.rgb[0] = t_rgb[0];
	lb.rgb[1] = t_rgb[1];
	lb.rgb[2] = t_rgb[2];

    listbs.push_back(lb);
    
    return (int)listbs.size() - 1;
}





/*---------------------------------------------------------------------------*/
int window::add_graphic(char* img_name, int g_w, int g_h, int g_x, int g_y)
{
	graphic gf;

	//make sure we are within the window's limits
    if (g_x + g_w > w)
       g_x = w - g_w;
    if (g_y + g_h > h)
       g_y = h - g_h;
       
    gf.x = x + g_x;
    gf.y = y + g_y;
    gf.h = g_h;
    gf.w = g_w;

	gf.skin_id = get_image_id(img_name);

	gfxs.push_back(gf);

	return (int)gfxs.size()-1;
}
 



 




/*---------------------------------------------------------------------------*/

 
                      
 /***
 * set the visibility of a window
 ***/
void window::set_visibility(bool p_visible) 
{
    visible = p_visible;
}
 
                      
 
 
 
/*---------------------------------------------------------------------------*/ 
 
 
                      
/***
 * set if the window can be dragged
 ***/
void window::set_window_drag(bool p_drag)
{
    wdrag = p_drag;
}




/*---------------------------------------------------------------------------*/


    
                      
bool window::can_drag()
{
    return wdrag;
}
              
     

     
/*---------------------------------------------------------------------------*/
     
     
     
              
/***
 *sets the text of a textarea
 ***/
void window::set_textarea_text(int text_id, std::string p_text)
{
    texts[text_id].text = p_text;
}
    
    
    
    
/*---------------------------------------------------------------------------*/
                      



/***
 * get_image_id() - get the id of an image
 * with the supplied name in the specified skin
 * returns -1 if the image is not found
 ***/
int window::get_image_id(std::string image)
{
    //get the images for the button
    for (int i = 0; i < (int)img_names.size(); ++i)
    {
        if (img_names[i] == image)
            return i;
    }
    
    //if we couldn't find the image return -1
    return -1;
}                 




/*---------------------------------------------------------------------------*/




/***
 * move() - moves the window to a new xy position
 ***/
void window::move(int px, int py)
{
     int viewport[4];
     glGetIntegerv (GL_VIEWPORT, viewport);
     
     py = viewport[3] - py;
     
     int diffx = 0;
     int diffy = 0;
     
     for (int i = 0; i < (int)buttons.size(); ++i)
     {
             diffx = buttons[i].x - x;
             diffy = buttons[i].y - y;
             buttons[i].x = px + diffx;
             buttons[i].y = py + diffy;
     }
     for (int i = 0; i< (int)texts.size(); ++i)
     {
             diffx = texts[i].x - x;
             diffy = texts[i].y - y;
             texts[i].x = px + diffx;
             texts[i].y = py + diffy;
     }             
             
     x = px;
     y = py;
}


